package com.study.study.nio;

import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * 使用Java nio进行数据的读写
 */
public class WriteWithNio {

    private static final String FILE_NAME = "D:\\a.log";

    private static final Logger LOGGER = LoggerFactory.getLogger(WriteWithNio.class);

    @Test
    public void writeNIO() throws IOException, InterruptedException {
        FileOutputStream fos = new FileOutputStream(FILE_NAME, true);
        FileChannel channel = fos.getChannel();
        int i = 50000000;
        while (i > 0) {
            JSONObject object = new JSONObject(32);
            object.put("name" + i, "zhangyi");
            object.put("name1", "zhangyi");
            object.put("name2", "zhangyi");
            object.put("name3", "zhangyi");
            object.put("name4", "zhangyi");
            object.put("name5", "zhangyi");
            object.put("name6", "zhangyi");
            object.put("name7", "zhangyi");
            object.put("name8", "zhangyi");
            object.put("name9", "zhangyi");
            object.put("name10", "zhangyi");
            object.put("name11", "zhangyi");
            object.put("name12", "zhangyi");
            object.put("name13", "zhangyi");
            object.put("name14", "zhangyi");
            object.put("name15", "zhangyi");
            object.put("name16", "zhangyi");
            object.put("name17", "zhangyi");
            object.put("name18", "zhangyi");
            object.put("name19", "zhangyi");
            object.put("name20", "zhangyi");
            object.put("name21", "zhangyi");
            object.put("name22", "zhangyi");
            object.put("name23", "zhangyi");
            object.put("name24", "zhangyi");
            object.put("name25", "zhangyi");
            ByteBuffer buf = ByteBuffer.wrap((object + "\r\n").getBytes());
            buf.put((object + "\r\n").getBytes());
            buf.flip();
            channel.write(buf);
            channel.force(true);
            Thread.sleep(5);
            System.out.println("准备下一次写入");
            i--;
        }
        channel.close();
        fos.close();

    }

    @Test
    public void readFileNIO() throws Exception {
        for (int j = 0; j < 10; j++) {
            new Thread(new Runnable() {
                @Override
                public void run() {

                    try {
                        File file = new File("D:\\a2000.log");
                        int i = 5;
                        Scanner scanner = new Scanner(new FileInputStream(file), StandardCharsets.UTF_8.name());
                        while (scanner.hasNext()) {
                            i--;
                            if (i < 1) {
                                LOGGER.info(scanner.next());
                                Thread.sleep(5);
                            } else {
                                scanner.next();
                            }
                        }
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, j + "task").start();
        }

    }
}
